package com.usfuture.chart.techanly.amount
{
	import com.usfuture.chart.techanly.AbstractTechProperty;
	import com.usfuture.chart.techanly.ITechAnlyChart;
	import com.usfuture.chart.util.GraphicsUtil;
	import com.usfuture.event.DataEvent;
	import com.usfuture.techanly.entity.Line;
	import com.usfuture.techanly.entity.Ma;
	import com.usfuture.techanly.entity.MaParams;
	import com.usfuture.util.NumberFormat;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;


	public class AmountChart extends EventDispatcher implements ITechAnlyChart
	{
		private var _canvas:Sprite;
		private var _data:Array;
		private var _cursor:Sprite=new Sprite;
		private var _canvasWidth:Number;
		private var _canvasHeigth:Number;
		private var _highVol:Number;
		private var _leftWidthX:Number; //K线图距画布左边的距离
		private var _topHeightY:Number=16; //K线图距画布上边的距离
		private var _bottomHeightY:Number=1; //K线图距画布下边的距离
		private var _width:Number; //K线宽
		private var _height:Number; //K线高
		private var _starPoint:Point; //K线开始位置
		private var leftLabels:Array=new Array; //左边的价格label数组
		private var ydivideNum:int=3; //y轴等分数
		private var baseLabel:TextField=new TextField();
		private var _maParamsArr:Array;
		private var _madata:Array;
		private var _techProperty:AbstractTechProperty;

		public function AmountChart(canvas:Sprite,maParamsArr:Array)
		{

			this._canvas=canvas;
			this._maParamsArr = maParamsArr;
			this._canvas.addChild(_cursor);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMovseMove);
			loadLeftLabel();
			measure();
		}

		public function measure():void
		{
			this._canvasWidth=_canvas.width;
			this._canvasHeigth=_canvas.height - 1;
			this._leftWidthX=AbstractTechProperty.leftWidthX;
			this._width=_canvasWidth - _leftWidthX;
			this._height=_canvasHeigth - _topHeightY - _bottomHeightY;
			this._starPoint=new Point(_leftWidthX, _topHeightY);

			for (var i=0; i < ydivideNum; i++)
			{
				var leftLabel:TextField=leftLabels[i] as TextField;
				leftLabel.x=0;
				leftLabel.y=_starPoint.y + _height * (i / ydivideNum) - 10;
				leftLabel.width=_leftWidthX;
			}
			baseLabel.x=0;
			baseLabel.y=_starPoint.y + _height - 15;
			baseLabel.width=_leftWidthX;
			repaint();
		}

		/**
		 * 加载左边label
		 */
		private function loadLeftLabel():void
		{

			var textFormat:TextFormat=new TextFormat;
			textFormat.align=TextFormatAlign.RIGHT;
			textFormat.bold=true;
			textFormat.color=0x716857;
			textFormat.font="Arial";
			textFormat.size=10;

			for (var i=0; i < ydivideNum; i++)
			{
				var leftLabel:TextField=new TextField();
				leftLabel.defaultTextFormat=textFormat;
				leftLabel.selectable=false;
				leftLabels.push(leftLabel);
				this._canvas.addChild(leftLabel);
			}
			baseLabel=new TextField;
			baseLabel.defaultTextFormat=textFormat;
			baseLabel..selectable=false;
			this._canvas.addChild(baseLabel);
		}


		public function paint():void
		{
			//画背景
			drawBackground();
			if (!_data || _data.length <= 0)
				return;
			//计算峰值
			calPeakValue();

			//画左边的数字
			drawLeftNum();
			//画图
			drawLine();
			//画无线
			drawMa();
		}

		public function drawBackground():void
		{
			//画矩形
			this._canvas.graphics.lineStyle(1, 0x626957, 1);
			this._canvas.graphics.beginFill(0xffffff, 0);
			this._canvas.graphics.drawRect(_leftWidthX, 0, _width, _canvasHeigth);

			//画虚线
			for (var i=0; i < ydivideNum; i++)
			{
				var beginPoint:Point=new Point(_starPoint.x,_starPoint.y + i / ydivideNum * _height);
				var endPoint:Point=new Point(_starPoint.x + _width,_starPoint.y + i / ydivideNum * _height);
				GraphicsUtil.drawDashed(this._canvas.graphics, beginPoint, endPoint, 5, 2);
			}
		}

		public function drawLeftNum():void
		{
			var base:int;
			var _highVol1:Number;
			_highVol1=_highVol;
			if (_highVol > 100000000)
			{
				_highVol1=_highVol / 1000;
				base=1000;
			}
			else if (_highVol > 1000000)
			{
				_highVol1=_highVol / 100;
				base=1000;
			}
			else
			{
				base=1;
			}

			for (var i=0; i < ydivideNum; i++)
			{
				var leftLabel:TextField=leftLabels[i] as TextField;
				leftLabel.text=Math.round(_highVol1 - _highVol1 * (i / ydivideNum))+'';
			}

			baseLabel.text="×" + base;
		}

		public function drawLine():void
		{
			if (techProperty.mode)
			{
				for (var i:int=(techProperty.startPos - techProperty.lineNum < 0 ? 0 : techProperty.startPos - techProperty.lineNum), j:int=0; i < techProperty.startPos; i++, j++)
				{
					if (i < 0)
						break;
					var dailyLine:Line=Line(_data[i]);

					//设置颜色
					if(dailyLine.closePrice>dailyLine.openPrice){
						this._canvas.graphics.lineStyle(0.1,0xD30006,1);
						this._canvas.graphics.beginFill(0xD30006,1);
					}else if(dailyLine.closePrice==dailyLine.openPrice){
						this._canvas.graphics.lineStyle(0.1,0x817B6D,1);
						this._canvas.graphics.beginFill(0x817B6D,1);
					}else{
						this._canvas.graphics.lineStyle(0.1,0x009303,1);
						this._canvas.graphics.beginFill(0x009303,1);
					}


					//画矩形
					var volRectPos:Number=calYAxis(dailyLine.volume);
					this._canvas.graphics.drawRect(_starPoint.x + j * (techProperty.lineWidth + techProperty.lineMergin), volRectPos, techProperty.lineWidth, (_starPoint.y + _height - volRectPos));
					this._canvas.graphics.endFill();

				}
			}
			else
			{
				for (var i:int=(techProperty.startPos - techProperty.lineNum < 0 ? 0 : techProperty.startPos - techProperty.lineNum), j:int=0; i < techProperty.startPos; i++, j++)
				{
					if (i < 0)
						break;
					var dailyLine:Line=Line(_data[i]);

					//设置颜色
					if (dailyLine.closePrice > dailyLine.openPrice)
					{
						this._canvas.graphics.lineStyle(0.1, 0xFF0000, 1);
						this._canvas.graphics.beginFill(0xFF0000, 1);
					}
					else
					{
						this._canvas.graphics.lineStyle(0.1, 0x33FFFF, 1);
						this._canvas.graphics.beginFill(0x33FFFF, 1);
					}

					//画直线	
					volRectPos=calYAxis(dailyLine.volume);
					this._canvas.graphics.moveTo(_starPoint.x + (j + 1) * (_width / techProperty.lineNum), volRectPos);
					this._canvas.graphics.lineTo(_starPoint.x + (j + 1) * (_width / techProperty.lineNum), _starPoint.y + _height);
					this._canvas.graphics.endFill();
				}
			}
		}
		
		/**
		 * 画均线
		 */
		public function drawMa():void{
			
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?1:techProperty.startPos-techProperty.lineNum+1),j:int=1;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var nowMas:Object = _madata[i];
				var preMas:Object=_madata[i-1<0?0:i-1];
				var preX:Number = techProperty.mode?_starPoint.x+(j)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin):_starPoint.x+(j)*(_width/techProperty.lineNum);
				var nowX:Number=techProperty.mode?_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin):_starPoint.x+(j+1)*(_width/techProperty.lineNum);
				for(var k:int=0;k<_maParamsArr.length;k++){
					var maParams:MaParams=_maParamsArr[k] as MaParams;
					if(preMas[maParams.ma]){
						var prePos:Number=calYAxis(preMas[maParams.ma]);
						var nowPos:Number=calYAxis(nowMas[maParams.ma]);
						var prePoint:Point = new Point(preX,prePos);
						var nowPoint:Point  = new Point(nowX,nowPos);
						if((prePoint.y<_starPoint.y&&nowPoint.y<_starPoint.y)||(prePoint.y>_starPoint.y+_height&&nowPoint.y>_starPoint.y+_height)){continue;}
						else if((prePoint.y<_starPoint.y&&nowPoint.y>_starPoint.y)||(prePoint.y>_starPoint.y&&nowPoint.y<_starPoint.y)){
							prePoint.y = _starPoint.y;
							prePoint.x = (prePoint.x-nowPoint.x)*(_starPoint.y-nowPoint.y)/(prePoint.y-nowPoint.y)+nowPoint.x;
						}
							
						else if((prePoint.y<(_starPoint.y+_height)&&nowPoint.y>(_starPoint.y+_height))||(prePoint.y>(_starPoint.y+_height)&&nowPoint.y<(_starPoint.y+_height))){
							prePoint.y = _starPoint.y+_height;
							prePoint.x = (prePoint.x-nowPoint.x)*(_starPoint.y+_height-nowPoint.y)/(prePoint.y-nowPoint.y)+nowPoint.x;
						}
						
						this._canvas.graphics.lineStyle(1,maParams.color,1)
						this._canvas.graphics.moveTo(prePoint.x,prePoint.y);
						this._canvas.graphics.lineTo(nowPoint.x,nowPoint.y);
						this._canvas.graphics.endFill();
					}
				}		
			}
		}

		public function calPeakValue():void
		{
			var line:Line=Line(_data[techProperty.startPos - 1]);
			_highVol=line.volume;
			for (var i:int=techProperty.startPos - 1; i >= (techProperty.startPos - techProperty.lineNum); i--)
			{
				if (i < 0)
					break;
				line=Line(_data[i]);
				if (line.volume > _highVol)
					_highVol=line.volume;
			}
		}

		public function calYAxis(value:Number):Number
		{
			return _starPoint.y + (_highVol - value) / _highVol * _height;
		}

		public function repaint():void
		{
			this._canvas.graphics.clear();
			paint();
		}

		public function get data():Array
		{
			return _data;
		}

		public function set data(value:Array):void
		{
			_data=value;
			_madata = new Array();
			
			for(var i:int=0;i<_data.length;i++){
				var line:Line = _data[i] as Line;
				var mas:Object = new Object();
				for(var k:int=0;k<_maParamsArr.length;k++){
					var maParams:MaParams = _maParamsArr[k] as MaParams;
					var ma:Ma = new Ma();
					if(i>=maParams.ma-1){
						var amount:Number=0;
						for(var j:int=i;j>=i-(maParams.ma-1);j--)
							amount+=Line(_data[j]).volume;
						ma.ma=amount/maParams.ma;
					}
					mas[maParams.ma]=ma.ma;
				}
				_madata.push(mas);
			}
			
		}


		public function onMovseMove(event:MouseEvent):void
		{
			if(!_data||_data.length==0)
				return;
			var pos:int=0;
			if(techProperty.mode)
				pos=(_canvas.mouseX-_leftWidthX)/(techProperty.lineWidth+techProperty.lineMergin);
			else
				pos=(_canvas.mouseX-_leftWidthX)/((_canvas.width-_leftWidthX)/techProperty.lineNum);
			var index:int = techProperty.startPos-techProperty.lineNum+pos>=_data.length?_data.length-1:techProperty.startPos-techProperty.lineNum+pos
			var line:Line=_data[index<0?0:index];
			var mas:Object=_madata[index<0?0:index];
			
			_cursor.graphics.clear();
			_cursor.graphics.lineStyle(1,0x000000);
			var xaxis:Number = 0;
			if(techProperty.mode)
				xaxis = _leftWidthX + pos*(techProperty.lineWidth+techProperty.lineMergin) + techProperty.lineWidth/2;
			else
				xaxis = _leftWidthX + pos*((_canvas.width-_leftWidthX)/techProperty.lineNum);
			var yaxis:Number = calYAxis(line.volume);
			
//			//横线
//			yaxis = Math.max(yaxis,0);
//			yaxis = Math.min(yaxis,_canvas.height);
//			_cursor.graphics.moveTo(_leftWidthX,yaxis);
//			_cursor.graphics.lineTo(_canvas.width,yaxis);
			//竖线
//			xaxis = Math.min(xaxis,_canvas.width);
//			xaxis =  Math.max(_leftWidthX,xaxis);
//			_cursor.graphics.moveTo(xaxis,0);
//			_cursor.graphics.lineTo(xaxis,_canvas.height); 
//			//小圆圈
//			_cursor.graphics.beginFill(0xFFFDE8,1);
//			_cursor.graphics.drawCircle(xaxis,yaxis,1);
//			_cursor.graphics.endFill();
			
			//派发当前鼠标所在的K线数据事件 
			dispatchEvent(new DataEvent(DataEvent.CURRENT_MOUSE_POSITION_DATA,{'line':line,'mas':mas}));
		}

		public function get techProperty():AbstractTechProperty
		{
			return _techProperty;
		}

		public function set techProperty(value:AbstractTechProperty):void
		{
			_techProperty = value;
			_techProperty.removeEventListener(AbstractTechProperty.CHANGE,change);
			_techProperty.addEventListener(AbstractTechProperty.CHANGE,change);
		}
		
		private function change(event:Event):void{
			repaint();
		}


	}
}